<template>
	<view class="c-wlist-iframe">
		<picker @change="bindPickerChange" mode="selector" :value="index" :range="arrWifi">
			<view class="c-wlist-add">
				<image src="@/static/images/icons/icon-add.png" mode="widthFix"></image>
				<view>新增热点</view>
			</view>
		</picker>
		<view class="c-wlist-list" v-for="(item,index) in WifiList" :key="index" v-show="WifiList.length!=0">
			<view class="c-wlist-item-name">
				<text v-show="(activeIndx==item.ID)?false:true">{{item.ClockWifiName}}</text>
				<input v-show="(activeIndx==item.ID)?true:false" @input="onInputName" type="text" v-model="item.ClockWifiName" />
			</view>
			<view class="c-wlist-item-action">
				<image @click="onStartEdit(item.ID,item.ClockWifiName)" src="@/static/images/icons/icon-edit.png" mode="widthFix"
				 v-show="(activeIndx==item.ID)?false:true"></image>
				<image @click="onSaveEdit(item.ID)" src="@/static/images/icons/icon-save.png" mode="widthFix" v-show="(activeIndx==item.ID)?true:false"></image>
				<image @click="onDele(item.ID)" src="@/static/images/icons/icon-dele.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="noWifiData" v-show="WifiList.length==0">
			<text>还没有设置 WIFI 记录</text>
		</view>
	</view>
</template>

<script>
	let pageApp = getApp();
	export default {
		data() {
			return {
				WifiList: [],
				arrWifi: [],
				index: 0,
				activeIndx: 0,
				ClockWifiName: ""
			}
		},
		onLoad: function() {
			// 获取wifi信息列表
			this.getWifiItems();
			// 获取设置wifi列表
			this.getWifiList();
		},
		methods: {
			onStartEdit(ID, ClockWifiName) {
				this.activeIndx = ID;
				this.ClockWifiName = ClockWifiName;
			},
			onInputName(e) {
				this.ClockWifiName = e.detail.value;
			},
			onSaveEdit(ID) {
				// 准备请求数据
				let apiData = {
					url: pageApp.globalData.url + "/ClockWifiUpdate",
					data: {
						"ID": ID,
						"ClockWifiName": this.ClockWifiName,
						"DepartmentID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0,
						"Note": "",
						"OrgID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0,
						"OrderNumber": 0
					},
					method: "POST",
					success: (d) => {
						console.log('数据', d);
						if (d.data.code == "001") {
							uni.showToast({
								"icon": "none",
								"title": "修改成功！"
							})
							setTimeout(() => {
								//重新请求一次数据
								this.activeIndx = 0;
							}, 1000)
						} else {
							uni.showToast({
								"icon": "none",
								"title": d.data.msg
							})
						}
					}
				}
				// 发送数据请求
				this.vjAjaxData(apiData);
			},
			onDele(ID) {
				uni.showModal({
					title: "微简系统",
					content: "真的要删除对应记录吗？",
					success: (res) => {
						if (res.confirm) {
							// 准备请求数据
							let apiData = {
								url: pageApp.globalData.url + "/ClockWifiDelete",
								data: {
									"ID": ID
								},
								method: "POST",
								success: (d) => {
									console.log('数据', d);
									if (d.data.code == "001") {
										uni.showToast({
											"icon": "none",
											"title": "删除成功！"
										})
										setTimeout(() => {
											//重新请求一次数据
											this.getWifiList();
										}, 1000)
									} else {
										uni.showToast({
											"icon": "none",
											"title": d.data.msg
										})
									}
								}
							}
							// 发送数据请求
							this.vjAjaxData(apiData);
						}
					},
				})
			},
			getWifiList() {
				// 准备请求数据
				let apiData = {
					url: pageApp.globalData.url + "/ClockWifiList",
					data: {
						"page": 1,
						"rows": 10,
						"sidx": "ClockWifiName",
						"sord": "",
						"action": ""
					},
					success: (d) => {
						// console.log("已有数据", d)
						if (d.data.records > 0) {
							this.WifiList = d.data.data.items;
						}
					}
				}
				// 发送数据请求
				this.vjAjaxData(apiData);
			},
			bindPickerChange: function(e) {
				this.index = e.target.value;
				let _strSelectedWifi = this.arrWifi[this.index]
				if (_strSelectedWifi && _strSelectedWifi.length > 0) {
					let _arrSplitWifi = _strSelectedWifi.split(" , ");
					// 准备请求数据
					let apiData = {
						url: pageApp.globalData.url + "/ClockWifiNew",
						data: {
							"ClockWifiName": _arrSplitWifi[0],
							"SSID": _arrSplitWifi[0],
							"BSSID": _arrSplitWifi[1],
							"DepartmentID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0,
							"Note": "",
							"OrgID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0,
							"OrderNumber": 0
						},
						method: "POST",
						success: (d) => {
							console.log('数据', d);
							if (d.data.code == "001") {
								uni.showToast({
									"icon": "none",
									"title": "增加成功！"
								})
								setTimeout(() => {
									//重新请求一次数据
									this.getWifiList();
								}, 1000)
							} else {
								uni.showToast({
									"icon": "none",
									"title": d.data.msg
								})
							}
						}
					}
					// 发送数据请求
					this.vjAjaxData(apiData);
				} else {
					uni.showToast({
						icon: "none",
						title: "获取WIFI列表失败!"
					});
				}
			},
			getWifiItems() {
				// #ifdef APP-PLUS
				// 主窗体  
				let MainActivity = plus.android.runtimeMainActivity();
				// 上下文  
				let Context = plus.android.importClass('android.content.Context');
				// 导入WIFI管理 和 WIFI 信息 的class  
				plus.android.importClass("android.net.wifi.WifiManager");
				plus.android.importClass("android.net.wifi.WifiInfo");
				plus.android.importClass("android.net.wifi.ScanResult");
				plus.android.importClass("java.util.ArrayList");
				// 获取 WIFI 管理实例  
				let wifiManager = MainActivity.getSystemService(Context.WIFI_SERVICE);
				wifiManager.setWifiEnabled(true);
				wifiManager.startScan();
				let rs = wifiManager.getScanResults();
				let len = rs.size();
				let _item = "";
				for (let i = 0; i < len; i++) {
					if (rs.get(i).plusGetAttribute('SSID')) {
						_item = rs.get(i).plusGetAttribute('SSID') + " , " + rs.get(i).plusGetAttribute('BSSID');
						this.arrWifi.push(_item);
					}
				}
				// #endif

				// #ifndef APP-PLUS
				this.arrWifi = this.WifiList;
				// #endif
			}
		}
	}
</script>

<style>
	page {
		background-color: #F1F5F6;
	}

	.c-wlist-iframe {
		margin: 20rpx 0;
	}

	.c-wlist-list {
		background-color: #ffffff;
		padding: 30rpx;
		border-bottom: solid 2rpx #F1F5F6;
		display: flex;
		justify-content: space-between;
	}

	.c-wlist-item-action {
		width: 100rpx;
		display: flex;
		justify-content: space-between;
	}

	.c-wlist-item-action image {
		width: 38rpx;
	}

	.noWifiData {
		padding: 30rpx;
		margin: 20rpx 0;
		text-align: center;
	}

	.c-wlist-add {
		display: flex;
		background-color: #ffffff;
		padding: 30rpx;
		margin: 20rpx 0;
		color: #4497e1;
		font-weight: 700;
	}

	.c-wlist-add image {
		margin-top: 6rpx;
		width: 38rpx;
		margin-right: 16rpx;
	}

	.c-wlist-item-name input {
		padding: 10rpx 0;
		border: solid 2rpx #F1F5F6;
	}
</style>
